home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test17.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  38KB  |  1,560 lines

  1. /* Comment on usage and program: ark!/mnt/rene/prac/os/unix/comment.changes */
  2.  
  3. /* "const.h", created by Rene Montsma and Menno Wilcke */
  4.  
  5.  
  6. #include <sys/types.h>        /* type defs */
  7. #include <sys/stat.h>        /* struct stat */
  8. #include <errno.h>        /* the error-numbers */
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11.  
  12. #define NOCRASH 1        /* test11(), 2nd pipe */
  13. #define PDPNOHANG  1        /* test03(), write_standards() */
  14.  
  15. #define USER_ID   12
  16. #define GROUP_ID   1
  17. #define FF        3        /* first free filedes. */
  18. #define USER      1        /* uid */
  19. #define GROUP     0        /* gid */
  20.  
  21. #define ARSIZE   256        /* array size */
  22. #define PIPESIZE 3584        /* maximum number of bytes to be * written on
  23.              * pipe               */
  24.  
  25. #define MAXOPEN  17        /* maximum number of extra open files */
  26.  
  27. #define MAXLINK 0177        /* maximum number of links per file */
  28. #define LINKCOUNT 5
  29.  
  30. #define MASK    0777        /* selects lower nine bits */
  31.  
  32. #define END_FILE     0        /* returned by read-call at eof */
  33.  
  34. #define OK      0
  35. #define FAIL   -1
  36.  
  37. #define R       0        /* read (open-call) */
  38. #define W       1        /* write (open-call) */
  39. #define RW      2        /* read & write (open-call) */
  40.  
  41. #define RWX     7        /* read & write & execute (mode) */
  42.  
  43. #define NIL     ""
  44. #define UMASK   "umask"
  45. #define CREAT   "creat"
  46. #define WRITE   "write"
  47. #define READ    "read"
  48. #define OPEN    "open"
  49. #define CLOSE   "close"
  50. #define LSEEK   "lseek"
  51. #define ACCESS  "access"
  52. #define CHDIR   "chdir"
  53. #define CHMOD   "chmod"
  54. #define LINK    "link"
  55. #define UNLINK  "unlink"
  56. #define PIPE    "pipe"
  57. #define STAT    "stat"
  58. #define FSTAT   "fstat"
  59. #define DUP     "dup"
  60. #define UTIME   "utime"
  61.  
  62. extern int errno;        /* the error-number */
  63. int errct;
  64.  
  65. long lseek();
  66.  
  67. char *file[];
  68. extern char *strcpy(), *strcat();
  69. char *fnames[];
  70. char *dir[];
  71.  
  72. /* "decl.c", created by Rene Montsma and Menno Wilcke */
  73.  
  74. /* Used in open_alot, close_alot */
  75. char *file[20] = {"f0", "f1", "f2", "f3", "f4", "f5", "f6",
  76.       "f7", "f8", "f9", "f10", "f11", "f12", "f13",
  77.       "f14", "f15", "f16", "f17", "f18", "f19"}, *fnames[8] = {"---", "--x", "-w-", "-wx", "r--",
  78.                                    "r-x", "rw-", "rwx"}, *dir[8] = {"d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x",
  79.                             "drw-", "drwx"};
  80.  /* Needed for easy creating and deleting of directories */
  81.  
  82. /* "test.c", created by Rene Montsma and Menno Wilcke */
  83.  
  84.  
  85. /*****************************************************************************
  86.  *                              TEST                                         *
  87.  ****************************************************************************/
  88. main(argc, argv)
  89. int argc;
  90. char *argv[];
  91. {
  92.   int n, mask;
  93.  
  94.   mask = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
  95.  
  96.   if (fork()) {
  97.     printf("Test 17 ");
  98.  
  99.     wait(&n);
  100.     clean_up_the_mess();
  101.     if (errct == 0)
  102.         printf("ok\n");
  103.     else
  104.         printf("%d errors\n", errct);
  105.     exit(0);
  106.   } else {
  107.     test(mask);
  108.     exit(0);
  109.   }
  110. }
  111.  
  112.  
  113.  
  114. test(mask)
  115. int mask;
  116. {
  117.   int n;
  118.   umask(0);            /* not honest, but i always forget */
  119.  
  120.   if (mask & 00001) test01();
  121.   if (mask & 00002) make_and_fill_dirs();
  122.   if (mask & 00004) test02();
  123.   if (mask & 00010) test08();
  124.   if (mask & 00020) test09();
  125.   if (mask & 00040) test10();
  126.   if (mask & 00100) test11();
  127.   if (mask & 00200) test12();
  128.   if (mask & 00400) test13();
  129.   if (mask & 01000) test14();
  130.   umask(022);
  131. }                /* test */
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /* "t1.c" created by Rene Montsma and Menno Wilcke */
  139.  
  140. /*****************************************************************************
  141.  *                              test UMASK                                   *
  142.  ****************************************************************************/
  143. test01()
  144. {
  145.   int oldvalue, newvalue, tempvalue;
  146.   int nr;
  147.  
  148.   if ((oldvalue = umask(0777)) != 0) err(0, UMASK, NIL);
  149.  
  150.   /* Special test: only the lower 9 bits (protection bits) may part- *
  151.    * icipate. ~0777 means: 111 000 000 000. Giving this to umask must*
  152.    * not change any value.                                           */
  153.  
  154.   if ((newvalue = umask(~0777)) != 0777) err(1, UMASK, "illegal");
  155.   if (oldvalue == newvalue) err(11, UMASK, "not change mask");
  156.  
  157.   if ((tempvalue = umask(0)) != 0) err(2, UMASK, "values");
  158.  
  159.  
  160.   /* Now test all possible modes of umask on a file */
  161.   for (newvalue = MASK; newvalue >= 0; newvalue -= 0111) {
  162.     tempvalue = umask(newvalue);
  163.     if (tempvalue != oldvalue) {
  164.         err(1, UMASK, "illegal");
  165.         break;        /* no use trying more */
  166.     } else if ((nr = creat("file01", 0777)) < 0)
  167.         err(5, CREAT, "'file01'");
  168.     else {
  169.         try_close(nr, "'file01'");
  170.         if (get_mode("file01") != (MASK & ~newvalue))
  171.             err(7, UMASK, "mode computed");
  172.         try_unlink("file01");
  173.     }
  174.     oldvalue = newvalue;
  175.   }
  176.  
  177.   /* The loop has terminated with umask(0) */
  178.   if ((tempvalue = umask(0)) != 0)
  179.     err(7, UMASK, "umask may influence rest of tests!");
  180. }                /* test01 */
  181.  
  182.  
  183.  
  184. /*****************************************************************************
  185.  *                              test CREAT                                   *
  186.  ****************************************************************************/
  187. test02()
  188. {
  189.   int n, n1, mode;
  190.   char a[ARSIZE], b[ARSIZE];
  191.   struct stat stbf1;
  192.  
  193.   mode = 0;
  194.   /* Create twenty files, check filedes */
  195.   for (n = 0; n < MAXOPEN; n++) {
  196.     if (creat(file[n], mode) != FF + n)
  197.         err(13, CREAT, file[n]);
  198.     else {
  199.         if (get_mode(file[n]) != mode)
  200.             err(7, CREAT, "mode set while creating many files");
  201.  
  202.         /* Change  mode of file to standard mode, we want to *
  203.          * use a lot (20) of files to be opened later, see   *
  204.          * open_alot(), close_alot().                        */
  205.         if (chmod(file[n], 0700) != OK) err(5, CHMOD, file[n]);
  206.  
  207.     }
  208.     mode = (mode + 0100) % 01000;
  209.   }
  210.  
  211.   /* Already twenty files opened; opening another has to fail */
  212.   if (creat("file02", 0777) != FAIL)
  213.     err(9, CREAT, "created");
  214.   else
  215.     check(CREAT, EMFILE);
  216.  
  217.   /* Close all files: seems blunt, but it isn't because we've  *
  218.    * checked all fd's already                                  */
  219.   if ((n = close_alot(MAXOPEN)) < MAXOPEN) err(5, CLOSE, "MAXOPEN files");
  220.  
  221.   /* Creat 1 file twice; check */
  222.   if ((n = creat("file02", 0777)) < 0)
  223.     err(5, CREAT, "'file02'");
  224.   else {
  225.     init_array(a);
  226.     if (write(n, a, ARSIZE) != ARSIZE) err(1, WRITE, "bad");
  227.  
  228.     if ((n1 = creat("file02", 0755)) < 0)    /* receate 'file02' */
  229.         err(5, CREAT, "'file02' (2nd time)");
  230.     else {
  231.         /* Fd should be at the top after recreation */
  232.         if (lseek(n1, 0L, SEEK_END) != 0)
  233.             err(11, CREAT, "not truncate file by recreation");
  234.         else {
  235.             /* Try to write on recreated file */
  236.             clear_array(b);
  237.  
  238.             if (lseek(n1, 0L, SEEK_SET) != 0)
  239.                 err(5, LSEEK, "to top of 2nd fd 'file02'");
  240.             if (write(n1, a, ARSIZE) != ARSIZE)
  241.                 err(1, WRITE, "(2) bad");
  242.  
  243.             /* In order to read we've to close and open again */
  244.             try_close(n1, "'file02'  (2nd creation)");
  245.             if ((n1 = open("file02", RW)) < 0)
  246.                 err(5, OPEN, "'file02'  (2nd recreation)");
  247.  
  248.             /* Continue */
  249.             if (lseek(n1, 0L, SEEK_SET) != 0)
  250.                 err(5, LSEEK, "to top 'file02'(2nd fd) (2)");
  251.             if (read(n1, b, ARSIZE) != ARSIZE)
  252.                 err(1, READ, "wrong");
  253.  
  254.             if (comp_array(a, b, ARSIZE) != OK) err(11, CREAT,
  255.                     "not really truncate file by recreation");
  256.         }
  257.         if (get_mode("file02") != 0777)
  258.             err(11, CREAT, "not maintain mode by recreation");
  259.         try_close(n1, "recreated 'file02'");
  260.  
  261.     }
  262.     remove(n, "file02");
  263.   }
  264.  
  265.   /* Give 'creat' wrong input: dir not searchable */
  266.   if (creat("drw-/file02", 0777) != FAIL)
  267.     err(4, CREAT, "'drw-'");
  268.   else
  269.     check(CREAT, EACCES);
  270.  
  271.   /* Dir not writable */
  272.   if (creat("dr-x/file02", 0777) != FAIL)
  273.     err(12, CREAT, "'dr-x/file02'");
  274.   else
  275.     check(CREAT, EACCES);
  276.  
  277.   /* File not writable */
  278.   if (creat("drwx/r-x", 0777) != FAIL)
  279.     err(11, CREAT, "recreate non-writable file");
  280.   else
  281.     check(CREAT, EACCES);
  282.  
  283.   /* Try to creat a dir */
  284.   if ((n = creat("dir", 040777)) != FAIL) {
  285.     if (fstat(n, &stbf1) != OK)
  286.         err(5, FSTAT, "'dir'");
  287.     else if (stbf1.st_mode != 0100777)
  288.         err(11, CREAT, "'creat' a new directory");
  289.     remove(n, "dir");
  290.   }
  291.  
  292.   /* We don't consider it to be a bug when creat * does not accept
  293.    * tricky modes                */
  294.  
  295.   /* File is an existing dir */
  296.   if (creat("drwx", 0777) != FAIL)
  297.     err(11, CREAT, "create an existing dir!");
  298.   else
  299.     check(CREAT, EISDIR);
  300. }                /* test02 */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. test08()
  308. {
  309.  
  310.  
  311.   /* Test chdir to searchable dir */
  312.